package com.sinodata.bsm.cicp.finder.network;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.Snmp;

import com.sinodata.bsm.cicp.util.snmp.GetData;
import com.sinodata.bsm.cicp.util.snmp.MibSystemEntry;
import com.sinodata.bsm.cicp.util.snmp.OMMappingInfo;

/**
 * <p>
 * Description: 网络设备主机发现类
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-9-3 下午3:48:13          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class HostDeviceFinder extends NetWorkFinder {

    private static final Logger logger = Logger.getLogger(HostDeviceFinder.class);

    @Override
    public List<String[]> find(Snmp snmp, CommunityTarget target) {

        List<String[]> resultList = new ArrayList<String[]>();
        try {
            List<OMMappingInfo> os = GetData.getAllTableData(snmp, MibSystemEntry.class, target);
            if (os == null || os.size() < 1) {
                throw new Exception("发现网络设备[" + ip + "]异常，程序使用的oid没有获取到相关数据");
            }
            MibSystemEntry system = (MibSystemEntry) os.get(0);
            String sysoid = system.getSysObjectID();
            String[] resultArray = new String[3 + getAttributes().length];
            resultList.add(resultArray);
            resultArray[0] = ip + "_" + sysoid; //资源标示
            resultArray[1] = system.getSysName();//资源名称
            resultArray[2] = system.getSysDescr().split("\n")[0];//资源描述
            resultArray[3] = system.getSysObjectID();
            resultArray[4] = getCompanyBySysoid(sysoid);
            resultArray[5] = system.getSysDescr().replaceAll("\n", ",");
        } catch (Exception e) {
            logger.error(ip + " 网络主机发现失败", e);
        }
        return resultList;
    }

    @Override
    public String[] getAttributes() {
        return new String[] { "系统oid", "厂商", "设备描述" };
    }
}
